package com.itheima.case2.web;

import com.itheima.case2.pojo.po.Role;
import com.itheima.case2.pojo.vo.*;
import com.itheima.case2.service.RoleService;
import com.itheima.case2.utils.BaseController;
import com.itheima.case2.utils.BeansFactory;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/role/*")
public class RoleServlet extends BaseServlet {
    //查询所有角色信息
    public void queryAllRole(HttpServletRequest request, HttpServletResponse response) {
        try {
            //使用工厂类获取对象
            RoleService roleService = BeansFactory.getInstance("roleService");
            //调用方法查询
            List<Role> roleList = roleService.queryAllRole();
            //将查询到的数据返回给前端
            Result result = new Result(true, "角色信息查询成功", roleList);
            BaseController.printResult(response, result);
        } catch (Exception e) {
            e.printStackTrace();
            Result result = new Result(false, "角色信息查询失败");
            try {
                BaseController.printResult(response, result);
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        }
    }

    //查询角色和对应权限信息
    public void queryRoleAndPerByPage(HttpServletRequest request, HttpServletResponse response) {
        try {
            //从前端获取数据
            QueryPageBean pageBean = BaseController.parseJSON2Object(request, QueryPageBean.class);
            //使用工厂类获取对象
            RoleService roleService = BeansFactory.getInstance("roleService");
            //调用方法查询
            PageResult pageResult = roleService.queryRoleAndPerByPage(pageBean);
            //将查询到的数据返回给前端
            Result result = new Result(true, "角色信息分页查询成功！", pageResult);
            BaseController.printResult(response, result);
        } catch (Exception e) {
            e.printStackTrace();
            Result result = new Result(false, "角色信息分页查询失败！");
            try {
                BaseController.printResult(response, result);
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        }
    }

    //修改角色和对应权限信息
    public void updateRoleAndPer(HttpServletRequest request, HttpServletResponse response) {
        try {
            //使用工具类获取前端传入的数据
            UpdateRole updateRole = BaseController.parseJSON2Object(request, UpdateRole.class);
            //使用工厂创建对象并获取
            RoleService roleService = BeansFactory.getInstance("roleService");
            roleService.updateRoleAndPer(updateRole);
            Result result = new Result(true, "角色信息修改成功！");
            BaseController.printResult(response, result);
        } catch (Exception e) {
            e.printStackTrace();
            try {
                Result result = new Result(false, "角色信息修改失败！");
                BaseController.printResult(response, result);
            } catch (Exception ioException) {
                ioException.printStackTrace();
            }
        }
    }

    //添加角色和对应权限信息
    public void insertRoleAndPer(HttpServletRequest request, HttpServletResponse response) {
        try {
            //使用工具类获取前端传入的数据
            AddRole addRole = BaseController.parseJSON2Object(request, AddRole.class);
            //使用工厂创建对象并获取
            RoleService roleService = BeansFactory.getInstance("roleService");
            roleService.insertRoleAndPer(addRole);
            Result result = new Result(true, "恭喜你，角色添加成功！");
            BaseController.printResult(response, result);
        } catch (Exception e) {
            e.printStackTrace();
            try {
                Result result = new Result(false, "角色添加失败！");
                BaseController.printResult(response, result);
            } catch (Exception ioException) {
                ioException.printStackTrace();
            }
        }
    }

    //删除角色和对应权限信息
    public void deleteById(HttpServletRequest request, HttpServletResponse response) {
        try {
            //获取前端参数
            String roleId = request.getParameter("roleId");
            //使用工厂创建对象并获取
            RoleService roleService = BeansFactory.getInstance("roleService");
            roleService.deleteById(roleId);
            Result result = new Result(true, "恭喜你，角色删除成功！");
            BaseController.printResult(response,result);
        } catch (Exception e) {
            e.printStackTrace();
            try {
                Result result = new Result(false, "角色删除失败！");
                BaseController.printResult(response,result);
            } catch (Exception ioException) {
                ioException.printStackTrace();
            }
        }
    }

}
